package widget.dress.tool
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import widget.dress.mode.CutModel;

	public class DressCutUtil
	{
		public function DressCutUtil()
		{
			
		}
				
		public static function cutTagSource(source:DisplayObject,tagFrame:uint,cutWidth:uint,cutHeight:uint):Array
		{
			var tempBitmapData:BitmapData = new BitmapData(source.width,source.height,true,0);
			tempBitmapData.draw(source);
			var charBitmapArray:Array = new Array();
			var rect:Rectangle = new Rectangle(0,0,cutWidth,cutHeight);
			var copyPoint:Point = new Point(0,0);
			for (var i:int =0; i< 8; i++){
				var directArray : Array = new Array();
				for (var j:int =0; j< tagFrame; j++){
					
					directArray[j] = new Bitmap(new BitmapData(cutWidth,cutHeight,true,0));
					rect.x = i * cutWidth;
					rect.y = j * cutHeight;
					directArray[j].bitmapData.copyPixels(tempBitmapData,rect,copyPoint);
	
				}
				charBitmapArray.push(directArray);
			}
			return charBitmapArray;
		}
		
		public static function getCut(source:DisplayObject,width:Number = 130,height:Number = 103):Vector.<CutModel>
		{
			var tempFrame:Number = Math.round(source.width / width );
			var tempDir:Number = Math.round(source.height / height);
			
			//获取源数据
			var bitmapData:BitmapData = new BitmapData(source.width,source.height,true,0);
			bitmapData.draw(source);
			
			//创建裁剪框
			var rect:Rectangle = new Rectangle(0,0,width,height);
			var copyPoint:Point = new Point(0,0);
		
			//创建数组
			var dressList:Vector.<CutModel> = new Vector.<CutModel>;
			//开始裁剪
			for (var i:int =0; i< tempDir; i++)
			{
				//创建数据模型
				var cutModel:CutModel = new CutModel();
				for (var j:int =0; j< tempFrame; j++)
				{
					//创建位图
					var tempBData2:BitmapData = new BitmapData(width,height,true,0);
					
					rect.x = j * width;
					rect.y = i * height;
					tempBData2.copyPixels(bitmapData,rect,copyPoint);
					cutModel.addBitmapData(tempBData2);
				}
				dressList.push(cutModel);
			}
			return dressList;
		}
		
		/**
		 *删除角色的动画 
		 * @param tagArray
		 * 
		 */		
		public static function removeCut(tagArray:Array):void
		{
			if(tagArray != null)
			{
				var tempLength:uint = tagArray.length
				for(var i:uint = 0;i < tempLength;i++)
				{
					if(tagArray[i] != null)
					{
						tagArray[i] = null;
					}
				}
			}
		}
	}
}